package com.zj.healthmanage.controller;

import com.zj.healthmanage.entity.PageResult;
import com.zj.healthmanage.entity.QueryPageBean;
import com.zj.healthmanage.entity.Result;
import com.zj.healthmanage.pojo.Member;
import com.zj.healthmanage.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/member")
@Api(value = "会员信息",description = "会员信息")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @ApiOperation(value = "分页查询会员信息",httpMethod = "POST")
    @RequestMapping("/queryMemberByPage")
    public PageResult queryMemberByPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = memberService.queryMemberByPage(queryPageBean);
        return pageResult;
    }

    @ApiOperation(value = "新增会员信息",httpMethod = "POST")
    @RequestMapping("/addMember")
    public Result addMember(@RequestBody Member member) {
        try {
            memberService.insertSelective(member);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "新增会员失败");
        }return new Result(true,"新增会员成功");
    }

    @ApiOperation(value = "删除会员信息",httpMethod = "POST")
    @RequestMapping("/deleteMember")
    public Result deleteMember(Integer id) {
        try {
            memberService.deleteByPrimaryKey(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除会员失败");
        }
        return new Result(true, "删除会员成功");
    }

    @ApiOperation(value = "编辑会员信息",httpMethod = "POST")
    @RequestMapping("/editMember")
    public Result editMember(@RequestBody Member member) {
        try {
            memberService.updateByPrimaryKeySelective(member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "编辑会员失败");
        }
        return new Result(true,"编辑会员成功");
    }

    @ApiOperation(value = "查询单个会员",httpMethod = "POST")
    @RequestMapping("/queryMemberById")
    public Result queryMemberById(Integer id) {
        try {
            Member member = memberService.queryMemberById(id);
            return new Result(true, "查询会员成功",member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询会员失败");
        }
    }

    @ApiOperation(value = "导出选中的会员相关信息",httpMethod = "POST")
    @RequestMapping("/exportMemberInfo")
    public Result exportMemberInfo(Integer[] memberIdList, HttpServletRequest request, HttpServletResponse response ) throws IOException {
        //String filePath = request.getSession().getServletContext().getRealPath("template")+ File.separator+"member_template.xlsx";
        XSSFWorkbook excel = new XSSFWorkbook(new FileInputStream(new File("E:\\health\\src\\main\\webapp\\pc\\template\\member_template.xlsx")));
        //读取第一个工作表
        XSSFSheet sheet = excel.getSheetAt(0);
        List<Map<String, Object>> list = memberService.exportMemberInfo(memberIdList);
        int row = 1;
        for (Map<String, Object> map : list) {
            String name = (String) map.get("name");
            String sex = (String) map.get("sex");
            String idCard = (String) map.get("idCard");
            String phoneNumber = (String) map.get("phoneNumber");
            String username = (String) map.get("username");
            String password = (String) map.get("password");
            String email = (String) map.get("email");
            String birthday = (String) map.get("birthday");
            String maritalStatus = (String) map.get("maritalStatus");
            String profession = (String) map.get("profession");
            String educationalLevel = (String) map.get("educationalLevel");
            String medicalHistory = (String) map.get("medicalHistory");
            String geneticHistory = (String) map.get("geneticHistory");
            String surgicalHistory = (String) map.get("surgicalHistory");
            String regTime = (String) map.get("regTime");
            String modifyTime = (String) map.get("modifyTime");

            sheet.getRow(row).getCell(1).setCellValue(name);
            sheet.getRow(row).getCell(2).setCellValue(sex);
            sheet.getRow(row).getCell(4).setCellValue(idCard);
            sheet.getRow(row).getCell(3).setCellValue(phoneNumber);
            sheet.getRow(row).getCell(5).setCellValue(username);
            sheet.getRow(row).getCell(6).setCellValue(password);
            sheet.getRow(row).getCell(7).setCellValue(email);
            sheet.getRow(row).getCell(8).setCellValue(birthday);
            sheet.getRow(row).getCell(9).setCellValue(maritalStatus);
            sheet.getRow(row).getCell(10).setCellValue(profession);
            sheet.getRow(row).getCell(11).setCellValue(educationalLevel);
            sheet.getRow(row).getCell(12).setCellValue(medicalHistory);
            sheet.getRow(row).getCell(13).setCellValue(geneticHistory);
            sheet.getRow(row).getCell(14).setCellValue(surgicalHistory);
            sheet.getRow(row).getCell(15).setCellValue(regTime);
            sheet.getRow(row).getCell(16).setCellValue(modifyTime);
            row++;
        }
        OutputStream out = null;
        try {
            out = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("content-Disposition","attachment;filename=report.xlsx");
        excel.write(out);
        out.flush();
        out.close();
        excel.close();
        return new Result(true,"执行成功!");
    }
}
